<?php

namespace app\api\controller\draw;

use app\admin\model\MopanAcc;
use app\admin\model\MopanFavor;
use app\admin\model\MopanItem;
use app\api\controller\Base;
use app\api\controller\Utils;

/**
 * 魔盘接口
 */
class Mopan extends Base
{

    public $control_version = '0.0.0';

    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');

        $openid = $this->get_openid($gh_id, $code);

        if ($openid) {
            $acc = MopanAcc::get(['openid' => $openid]);
            if (!$acc) {
                $acc = new MopanAcc();
                $acc->openid = $openid;
                $acc->level = 1;
                $acc->save();
            }
            // 返回用户id,用户等级和openid
            $data['userid'] = $acc->id;
            $data['level'] = $acc->level;
            $data['openid'] = $acc->openid;

            $this->result('login', $data);
        }
        $this->error('error');
    }

    // 配置
    public function config($v)
    {
        $shareflag = true;

        $img_id = rand(1, 5);
        $img = 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/mopan/result/' . $img_id . '.jpg';

        if ($v == $this->control_version) {
            $shareflag = false;
            $img = 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/mopan/result/0.jpg';
        }

        $flags = array(
            'shareflag' => $shareflag,
        );

        $this->result($img, $flags);
    }

    // 分享回调(解锁功能)
    public function share_callback()
    {
        $openid = $this->request->post('openid');
        $acc = MopanAcc::get(['openid' => $openid]);
        $acc->level = 1;
        $acc->save();
        $this->result('level', $acc->level);
    }

    // 更新用户头像昵称接口
    public function update_acc_info()
    {
        $openid = $this->request->post('openid');
        $nickname = $this->request->post('nickname');
        $avatar = $this->request->post('avatar');

        $acc = MopanAcc::get(['openid' => $openid]);
        if ($acc) {
            if ($nickname) {
                $acc->nickname = $nickname;
            }
            if ($avatar) {
                $acc->avatar = $avatar;
            }
            if ($acc->save() !== false) {
                $this->result('', 1);
            }
        }
        $this->result('', 0);
    }

    // 获取用户详细信息
    public function get_acc_info()
    {
        $openid = $this->request->post('openid');

        $acc = MopanAcc::get(['openid' => $openid]);
        $acc->item = MopanItem::where(['acc_id' => $acc->id])->select();
        $favor = MopanFavor::where(['acc_id' => $acc->id])->select();
        $favorList = array();
        foreach ($favor as $f) {
            $i = MopanItem::get($f->favor_id);
            array_push($favorList, $i);
        }
        $acc->favor = $favorList;
        $this->result('acc', $acc);
    }

    // 上传文件
    public function upload_img()
    {
        $file = $this->request->file('file');

        $info = $file->move(APP_PATH . "/../public/uploads/images/");
        if ($info) {
            $filename = $info->getSaveName();
            $path = 'https://cdnxcx.nanxuncn.cn/uploads/images/' . $filename;
            return $path;
        } else {
            echo $file->getError();
            die();
        }
    }

    // 添加用户作品信息
    public function add_item()
    {
        $userid = $this->request->post('userid');
        $url = $this->request->post('url');
        $name = $this->request->post('name');

        if ($url) {
            $item = new MopanItem();
            $item->acc_id = $userid;
            $item->url = $url;
            $item->name = $name;
            return $item->save();
        }
    }

    // 获取展示图片列表
    public function get_item_list()
    {
        $v = $this->request->post('v');
        $page = $this->request->post('page');
        $list = MopanItem::limit(($page - 1) * 12, 12)->order('id', 'desc')->select();

        foreach ($list as $item) {
            $item->acc_id = MopanAcc::get($item->acc_id);
        }

        $this->result('list', $list);
    }

    // 获取随机展示图片列表
    public function get_ramdom_list()
    {
        $util = new Utils();

        $list = $util->get_rand(12, 'mopan_item');
        $random_list = array();
        foreach ($list as $item) {
            $item['acc_id'] = MopanAcc::get($item['acc_id']);
            array_push($random_list, $item);
        }
        $this->result('random', array_values($random_list));
    }

    // 查询是否收藏
    public function fetch_favor()
    {
        $favor_id = $this->request->post('favor_id');
        $acc_id = $this->request->post('acc_id');

        if ($f = MopanFavor::get([
            'acc_id' => $acc_id,
            'favor_id' => $favor_id
        ])) {
            $msg = 1;
        } else {
            $msg = 0;
        }
        $this->result($msg);
    }

    // 添加收藏
    public function add_favor()
    {
        $favor_id = $this->request->post('favor_id');
        $acc_id = $this->request->post('acc_id');

        $f = new MopanFavor();
        $f->data([
            'acc_id' => $acc_id,
            'favor_id' => $favor_id
        ]);
        return $f->save();
    }

    // 删除收藏
    public function del_favor()
    {
        $favor_id = $this->request->post('favor_id');
        $acc_id = $this->request->post('acc_id');

        $f = MopanFavor::get([
            'acc_id' => $acc_id,
            'favor_id' => $favor_id
        ]);
        return $f->delete();
    }
}
